package com.yayohos.locationmanager.base;


import com.yayohos.locationmanager.LocationManager;
import com.yayohos.locationmanager.configuration.LocationConfiguration;
import com.yayohos.locationmanager.helper.LogUtils;
import com.yayohos.locationmanager.listener.LocationListener;
import com.yayohos.locationmanager.providers.locationprovider.DefaultLocationProvider;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

/**
 * LocationBaseAbility
 *
 * @date 2021/2/5/26
 */
public abstract class LocationBaseAbility extends Ability implements LocationListener {
    private LocationManager locationManager;

    /**
     * getLocationConfiguration 用于自定义配置LocationConfiguration
     *
     * @return LocationConfiguration
     */
    public abstract LocationConfiguration getLocationConfiguration();

    /**
     * getLocationManager 获取LocationManager
     *
     * @return locationManager
     */
    protected LocationManager getLocationManager() {
        return locationManager;
    }

    /**
     * 开始定位
     */
    protected void getLocation() {
        if (locationManager != null) {
            locationManager.get();
        } else {
            throw new IllegalStateException("locationManager is null. "
                    + "Make sure you call super.initialize before attempting to getLocation");
        }
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        LogUtils.logI("onStart");
        locationManager = new LocationManager.Builder(getApplicationContext())
                .configuration(getLocationConfiguration())
                .locationProvider(new DefaultLocationProvider())
                .ability(this)
                .notify(this)
                .build();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        locationManager.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        locationManager.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationManager.onStop();
    }

    @Override
    public void onProcessTypeChanged(int processType) {
        // override if needed
    }

    @Override
    public void onPermissionGranted(boolean alreadyHadPermission) {
        // override if needed
    }


    @Override
    public void onStatusChanged(int status) {
        // override if needed
    }

    @Override
    public void onProviderEnabled(String provider) {
        // override if needed
    }

    @Override
    public void onProviderDisabled(String provider) {
        // override if needed
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        locationManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        locationManager.onAbilityResult(requestCode, resultCode, resultData);
    }
}
